//
//  HJWithdrawalRecordController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJWithdrawalRecordController.h"
#import "HJWithdrawalRecordCell.h"
#import "HJWithdrawalModel.h"

@interface HJWithdrawalRecordController ()
@property(nonatomic,strong) NSMutableArray <HJTransactionModel *>* dataArr;
@property(nonatomic,strong) NSMutableArray <HJWithdrawalModel *>* cashArr;
@property(nonatomic,assign) NSInteger page1;;
@end

@implementation HJWithdrawalRecordController
-(NSMutableArray<HJWithdrawalModel *> *)cashArr {
    if (!_cashArr) {
        _cashArr = [NSMutableArray array];
    }
    return _cashArr;
}
-(NSMutableArray <HJTransactionModel *> *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type==withdrawalType) {
         [self loadCash];
    }else {
        [self loadTransactionWithWork];
    }
    [self configRefresh];
}

-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    [self.tableView registerCellByNibName:@"HJWithdrawalRecordCell"];
}
- (void)configRefresh {
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithHeader)];
    self.tableView.mj_header = header;
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithFooter)];
    self.tableView.mj_footer = footer;
}

-(void)refreshDataWithHeader {
    self.page1 = 1;
    if (self.type==withdrawalType) {
         [self loadCash];
    }else {
        [self loadTransactionWithWork];
    }
}
-(void)refreshDataWithFooter {
   
    self.page1++;
    if (self.page1 > [self.totalPage integerValue]) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } else {
        if (self.type==withdrawalType) {
            [self loadCash];
        }else {
            [self loadTransactionWithWork];
        }
    }
}
-(void)loadCash {
    [TZHttpTool post:CashDetailAPI params:nil success:^(NSDictionary *result) {
        self.totalPage = result[@"data"][@"totalPage"];
        NSArray *arr = [HJWithdrawalModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"models"]];
        [self.cashArr removeAllObjects];
        [self.cashArr addObjectsFromArray:arr];
        [self.tableView reloadData];
        [self.tableView endRefresh];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
-(void)loadTransactionWithWork {
    [TZHttpTool post:TransDetailAPI params:@{@"page":@(self.page1)} success:^(NSDictionary *result) {
        self.totalPage = result[@"data"][@"totalPage"];
        NSArray *arr = [HJTransactionModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"models"]];
        if (self.page1==1) {
            [self.dataArr removeAllObjects];
        }
        [self.dataArr addObjectsFromArray:arr];
        [self.tableView reloadData];
        [self.tableView endRefresh];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.type==withdrawalType) {
        return self.cashArr.count;
    }else {
        return self.dataArr.count;
    }
   
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJWithdrawalRecordCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HJWithdrawalRecordCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.type==withdrawalType) {
        cell.withdrawalModel = self.cashArr[indexPath.row];
    }else {
        cell.transactionModel = self.dataArr[indexPath.row];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}


@end
